<div class="layui-card">
    <div class="layui-card-header">
        <div class="layui-btn-group">
            <a class="layui-btn layui-btn-sm newTab" lay-href="<?php echo \yii\helpers\Url::to(['create']) ?>" ><i class="layui-icon layui-icon-add-1"></i>添加表单</a>
            <button class="layui-btn layui-btn-sm layui-btn-normal" id="activeForm" ><i class="layui-icon layui-icon-ok"></i>激活表单</button>
            <button class="layui-btn layui-btn-sm layui-btn-danger" id="inActiveForm" ><i class="layui-icon layui-icon-close"></i>禁用表单</button>
        </div>
    </div>
    <div class="layui-card-body">
        <table id="dataTable" lay-filter="dataTable"></table>
        <script type="text/html" id="options">
            <a lay-event="edit" class="layui-btn layui-btn-xs" ><i class="layui-icon layui-icon-edit"></i>编辑</a>
            <a lay-event="destroy" class="layui-btn layui-btn-xs layui-btn-danger" ><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
    </div>
</div>


<?php
$url = yii\helpers\Url::to(['index']);
$status_url = yii\helpers\Url::to(['status']);
$js = <<<JS
    layui.use(['form','element','table', 'layer'],function() {
        var form = layui.form;  
        var element = layui.element;  
        var table = layui.table;  
        var layer = layui.layer;  
        
        var dataTable = table.render({
            elem: '#dataTable'
            ,height: 'full-200'
            ,url: '{$url}' 
            ,page: true 
            ,cols: [[ 
                {checkbox: true}
                ,{field: 'nombre', title: '名称'}
                ,{field: 'descripcion', title: '描述'}
                ,{field: 'status_name', title: '状态'}
                ,{title: '操作',toolbar:'#options'}
            ]]
        });
        
        //表单禁用、激活
        $("#activeForm").click(function() {
            var ids = []
            var hasCheck = table.checkStatus('dataTable')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length>0){
                $.each(hasCheckData,function (index,element) {
                    ids.push(element.id)
                })
            }
            if (ids.length>0){
                layer.confirm('确认激活吗？', function(index){
                    $.post("{$status_url}",{ids:ids,estatus:'A'},function (res) {
                        if (res.code==0){
                            dataTable.reload()
                        }
                        layer.close(index);
                        layer.msg(res.msg)
                    });
                })
            }else {
                layer.msg('请选择至少一项')
            }  
        })
        $("#inActiveForm").click(function() {
            var ids = []
            var hasCheck = table.checkStatus('dataTable')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length>0){
                $.each(hasCheckData,function (index,element) {
                    ids.push(element.id)
                })
            }
            if (ids.length>0){
                layer.confirm('确认禁用吗？', function(index){
                    $.post("{$status_url}",{ids:ids,estatus:'I'},function (res) {
                        if (res.code==0){
                            dataTable.reload()
                        }
                        layer.close(index);
                        layer.msg(res.msg)
                    });
                })
            }else {
                layer.msg('请选择至少一项')
            }  
        })
        
        //监听工具条
        table.on('tool(dataTable)', function(obj){ 
            var data = obj.data; 
            var layEvent = obj.event;
            if(layEvent === 'destroy'){
                layer.confirm('确认删除吗？', function(index){
                    $.post(data.destroy_url,{id:data.id},function(res) {
                        layer.msg(res.msg,{},function() {
                            if (res.code==0){
                                obj.del(); 
                                layer.close(index);
                            }   
                        })  
                    })
                });
            } else if(layEvent === 'edit'){ 
                location.href = data.edit_url
            }
        });
        
        
    });
JS;
$this->registerJs($js);
